// 简单的本地存储封装
const storage = {
    set: (key, value) => {
        localStorage.setItem(key, JSON.stringify(value));
    },

    get: (key) => {
        const value = localStorage.getItem(key);
        return value ? JSON.parse(value) : null;
    },

    remove: (key) => {
        localStorage.removeItem(key);
    }
};

// 表单验证工具
const validate = {
    phone: (phone) => {
        const re = /^1[3-9]\d{9}$/;
        return re.test(phone);
    },

    password: (password) => {
        return password.length >= 6;
    }
};

// 防抖函数
const debounce = (func, delay) => {
    let timeoutId;
    return function(...args) {
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => {
            func.apply(this, args);
        }, delay);
    };
};

// 节流函数
const throttle = (func, limit) => {
    let inThrottle;
    return function(...args) {
        if (!inThrottle) {
            func.apply(this, args);
            inThrottle = true;
            setTimeout(() => (inThrottle = false), limit);
        }
    };
};

// 显示提示信息
const showAlert = (message, type = 'info') => {
    // 移除现有的提示
    const existingAlert = document.querySelector('.alert');
    if (existingAlert) existingAlert.remove();

    const alert = document.createElement('div');
    alert.className = `alert alert-${type}`;
    alert.textContent = message;

    document.body.appendChild(alert);

    // 自动消失
    setTimeout(() => {
        alert.style.opacity = '0';
        setTimeout(() => {
            alert.remove();
        }, 300);
    }, 3000);
};

// 添加提示样式
const addAlertStyles = () => {
    const style = document.createElement('style');
    style.textContent = `
    .alert {
      position: fixed;
      top: 20px;
      left: 50%;
      transform: translateX(-50%);
      padding: 12px 24px;
      border-radius: 4px;
      color: white;
      font-size: 14px;
      z-index: 1000;
      opacity: 1;
      transition: opacity 0.3s;
      box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    }
    
    .alert-info {
      background-color: #909399;
    }
    
    .alert-success {
      background-color: #67C23A;
    }
    
    .alert-error {
      background-color: #F56C6C;
    }
    
    .alert-warning {
      background-color: #E6A23C;
    }
  `;
    document.head.appendChild(style);
};

// 初始化时添加样式
addAlertStyles();